package com.haidechizi.leetcode;

/**
 * @author: wangdejun
 * @create: 2020-04-13 11:35
 **/
public class _1_6_字符串压缩 {

    public String compressString(String S) {
        int count = 0;
        Character pre = null;
        char[] chars = S.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (Character character : chars) {
            if (pre == null) {
                pre = character;
                count = 1;
                continue;
            }
            if (character.equals(pre)) {
                count++;
            } else {
                sb.append(pre).append(count);
                count = 1;
                pre = character;
            }
        }
        sb.append(pre).append(count);
        if (sb.length() < S.length()) {
            return sb.toString();
        }
        return S;
    }

    public static void main(String[] args) {
        new _1_6_字符串压缩().compressString("aabcccccaa");
    }
}
